package com.lanou.musicbox.adapter.onlinemusic;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.Rankings;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by 杨海宁 on 15/10/21.
 * 网络歌曲_排行榜页面Adapter
 */
public class RankingListAdapter extends BaseAdapter {
    private List<Rankings.ContentEntity> content;
    private List<Rankings.ContentEntity.ContentEntityInfo> rankingInfos;
    private Context mContext;
    private String TAG = "lanou44";

    private ImageLoader imageLoader;

    public RankingListAdapter(List<Rankings.ContentEntity> datas, Context context) {
        content = datas;
        Log.i(TAG, "RankingFragmentAdapter_content " + content.get(0).getName());
        mContext = context;
        imageLoader = VolleySingleton.getInstance(mContext).getImageLoader();

    }

    @Override
    public int getCount() {
        return content != null && content.size() > 0 ? content.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return content != null && content.size() > 0 ? content.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            //绑定布局
            convertView = LayoutInflater.from(mContext).inflate(R.layout.ranking_listview, null);
            viewHolder.rankingTitleTv = (TextView) convertView.findViewById(R.id.ranking_title_Tv);
            viewHolder.rankingOneIv = (ImageView) convertView.findViewById(R.id.ranking_one_Iv);
            viewHolder.rankingFirstSongTv = (TextView) convertView.findViewById(R.id.ranking_firstSong_Tv);
            viewHolder.rankingFirstAuthorTv = (TextView) convertView.findViewById(R.id.ranking_firstAuthor_Tv);
            viewHolder.rankingSecondSongTv = (TextView) convertView.findViewById(R.id.ranking_secondSong_Tv);
            viewHolder.rankingSecondAuthorTv = (TextView) convertView.findViewById(R.id.ranking_secondAuthor_Tv);
            viewHolder.rankingThirdSongTv = (TextView) convertView.findViewById(R.id.ranking_thirdSong_Tv);
            viewHolder.rankingThirdAuthorTv = (TextView) convertView.findViewById(R.id.ranking_thirdAuthor_Tv);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        rankingInfos = content.get(position).getContent();
        if (content != null && rankingInfos != null) {


            viewHolder.rankingTitleTv.setText(content.get(position).getName());
            viewHolder.rankingFirstSongTv.setText(rankingInfos.get(0).getTitle());
            viewHolder.rankingFirstAuthorTv.setText(rankingInfos.get(0).getAuthor());
            viewHolder.rankingSecondSongTv.setText(rankingInfos.get(1).getTitle());
            viewHolder.rankingSecondAuthorTv.setText(rankingInfos.get(1).getAuthor());
            viewHolder.rankingThirdSongTv.setText(rankingInfos.get(2).getTitle());
            viewHolder.rankingThirdAuthorTv.setText(rankingInfos.get(2).getAuthor());
            //设置图片
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(
                    viewHolder.rankingOneIv, R.mipmap.loading, R.mipmap.loading);
            imageLoader.get(content.get(position).getPic_s192(), imageListener);
        }

        return convertView;
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'ranking_listview.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    public class ViewHolder {

        private ImageView rankingOneIv;

        private TextView rankingTitleTv;

        private TextView rankingFirstSongTv;

        private TextView rankingFirstAuthorTv;

        private TextView rankingSecondSongTv;

        private TextView rankingSecondAuthorTv;

        private TextView rankingThirdSongTv;

        private TextView rankingThirdAuthorTv;


    }
}

